package com.chuyu.cy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuyu.cy.contants.PageResult;
import com.chuyu.cy.contants.ResponseCodeEnum;
import com.chuyu.cy.entity.domain.ChatMessage;
import com.chuyu.cy.entity.vo.MessageVO;
import com.chuyu.cy.exception.CustomException;
import com.chuyu.cy.mapper.ChatMessageMapper;
import com.chuyu.cy.service.ChatMessageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
* @author 周小白
* @description 针对表【chat_message】的数据库操作Service实现
*/
@Service
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ChatMessage>
    implements ChatMessageService{

    @Resource
    private ChatMessageMapper chatMessageMapper;


    /**
     * 获取聊天记录
     * @param userId the user
     * @param contactorId the contactor
     * @param pageNum the page number
     * @param pageSize the page size
     * @return the message list
     */
    @Override
    public PageResult<MessageVO> getMessageList(Integer userId, Integer contactorId, Integer pageNum, Integer pageSize) {
        if (userId == null || contactorId == null || pageNum == null) {
            throw new CustomException("userId or contactorId or pageNum is null");
        }

        LambdaQueryWrapper<ChatMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMessage::getSenderId, userId)
                    .eq(ChatMessage::getReceiverId, contactorId)
                    .or()
                    .eq(ChatMessage::getSenderId, contactorId)
                    .eq(ChatMessage::getReceiverId, userId)
                    .orderByDesc(ChatMessage::getCreateTime);

        Page<ChatMessage> page = new Page<>(pageNum, pageSize);
        Page<ChatMessage> selectPage = chatMessageMapper.selectPage(page, queryWrapper);

        ArrayList<MessageVO> list = new ArrayList<>();
        for (ChatMessage item : selectPage.getRecords()) {
            MessageVO messageVO = BeanUtil.copyProperties(item, MessageVO.class);
            list.add(messageVO);
        }

        PageResult<MessageVO> pageResult = new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal(selectPage.getTotal());

        return pageResult;
    }


    /**
     * 保存消息
     * @param messageVO the message
     * @return the result
     */
    @Override
    public MessageVO saveMessage(MessageVO messageVO) {
        if (messageVO == null) {
            throw new CustomException(ResponseCodeEnum.NO_PARAM);
        }
        if (messageVO.getSenderId() == null || messageVO.getReceiverId() == null
                || messageVO.getMessage() == null) {
            throw new CustomException(ResponseCodeEnum.ERROR_PARAM);
        }

        ChatMessage chatMessage = BeanUtil.copyProperties(messageVO, ChatMessage.class);
        int insert = chatMessageMapper.insert(chatMessage);
        if (insert <= 0) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        BeanUtil.copyProperties(chatMessage, messageVO);

        return messageVO;
    }
}




